package com.jason.apps.buyer;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.LoginActivity;
import com.apps.realtyportal.R;
import com.forimageandlist.Adapter;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.forimageandlist.ImageLoader;


public class Profile extends SherlockFragment{
  
	Adapter adapter;
	ConsumeWebService c;
	private TextView name;
	private TextView contact;
	private TextView email;
	private ImageView image;
	private Button editme;
	private ImageLoader imageloader;
	
	
	final static ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
	
	HashMap<String, String> map;
	SherlockFragmentActivity fa;

	public void displayList(){
		c = new ConsumeWebService();
		 String data = c.getMyAccount(LoginActivity.id);
		 
		 try {

				JSONArray array = new JSONArray(data);
				
				for (int i = 0; i < array.length(); i++) {
					map = new HashMap<String, String>();
					JSONObject e = array.getJSONObject(i);
					map.put("firstname", e.getString("firstname"));
					map.put("lastname", e.getString("lastname"));
					map.put("email", e.getString("email"));
					map.put("mobile", e.getString("mobile"));
					map.put("landline", e.getString("landline"));
					map.put("image", e.getString("image"));
					map.put("username", e.getString("username"));
					map.put("password", e.getString("password"));
										
					mylist.add(map);
				}
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString()+"diri jud");
			}finally{
				
			}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.myaccount, container, false);
		fa=(SherlockFragmentActivity) super.getActivity();
		name=(TextView)view.findViewById(R.id.my_name);
		contact=(TextView)view.findViewById(R.id.my_contact);
		email=(TextView)view.findViewById(R.id.my_email);
		image=(ImageView)view.findViewById(R.id.my_image);
		editme=(Button)view.findViewById(R.id.editme);
		
		 imageloader=new ImageLoader(getActivity().getApplicationContext());
		new Load().execute();
		 editme.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle data = new Bundle();
				
				data.putString("firstname",map.get("firstname"));
				data.putString("lastname",map.get("lastname"));
				data.putString("email",map.get("email"));
				data.putString("mobile",map.get("mobile"));
				data.putString("landline",map.get("landline"));
				data.putString("image",map.get("image"));
				data.putString("username",map.get("username"));
				data.putString("password",map.get("password"));
				
				Fragment fragment = new EditMe();
				fragment.setArguments(data);
				
				FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.buyer_fragment, fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(null);
                ft.commit();
			}
		});
		mylist.clear();

		return view;
    }
	 public class Load extends AsyncTask<Void, Void, Void> {

	     private final ProgressDialog dialog = new ProgressDialog(fa);

	  	    protected void onPreExecute() {

	  	            this.dialog.setMessage("Loading...");
	  	            this.dialog.show();
	  	    }
		    protected Void doInBackground(final Void... unused) {
		    	
				try {
					displayList();	
			
				} catch (Exception e) {
					Log.e("","wala jud ma add",e);
				}
		   
		        return null; 
		    }
		    protected void onPostExecute(Void result) {
		    	
		    	name.setText("Name: "+map.get("firstname")+" "+map.get("lastname"));
		    	contact.setText("Contact: "+map.get("mobile"));
		    	email.setText("Email: "+map.get("email"));
		    	imageloader.DisplayImage(map.get("image"), image);
				if (this.dialog!=null) {
		            this.dialog.dismiss();
		            }  
		     }
		  }	    
    
    
}